package com.psmz.iwi.docanalyzer.core.document.job;

import java.util.Collection;
import java.util.Observable;

import com.psmz.iwi.docanalyzer.core.document.business.DocumentRepositoryService;
import com.psmz.iwi.docanalyzer.core.document.business.DocumentRepositoryServiceException;
import com.psmz.iwi.docanalyzer.core.document.entities.Document;

/**
 * Zadanie indeksowania dokumentów
 * 
 * @author piotrs10
 * 
 */
public class DocumentIndexingJob extends Observable implements Runnable {

	public static final int START_NOTIFICATION = 1;
	public static final int FINISHED_NOTIFICATION = 2;
	public static final int FAILED_NOTIFICATION = 3;

	private DocumentRepositoryService documentRepositoryService;

	public DocumentIndexingJob(
			DocumentRepositoryService documentRepositoryService) {
		this.documentRepositoryService = documentRepositoryService;
	}

	@Override
	public void run() {
		System.out.println("Rozpoczeto indeksowanie");
		setChanged();
		notifyObservers(START_NOTIFICATION);
		try {
			Collection<Document> notIndexedDocuments = documentRepositoryService
					.getNotIndexedDocuments();
			documentRepositoryService.indexDocuments(notIndexedDocuments);
			System.out.println("Zakonczono indeksowanie");
			setChanged();
			notifyObservers(FINISHED_NOTIFICATION);
		} catch (DocumentRepositoryServiceException e) {
			System.err.println("Blad wykonania zadania indeksowania");
			System.err.println(e.toString());
			setChanged();
			notifyObservers(FAILED_NOTIFICATION);
		}
	}

}
